package py.edu.ucom.cadira.net.serializables;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

import py.edu.ucom.cadira.logic.Mesa;
import py.edu.ucom.cadira.net.XMLSerializable;

public class ListTableResponse implements XMLSerializable {
    private ArrayList<Mesa> mesas;
    private static final String NODE_LIST_TABLE = "listMesas";
    private String type = "pruebaXML";

    public ListTableResponse() {
    	super();
        this.mesas = new ArrayList<Mesa>();
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public void setMesas(ArrayList<Mesa> mesas) {
        this.mesas = mesas;
    }
    public ArrayList<Mesa> getMesas() {
        return this.mesas;
    }
    public void addMesa(Mesa mesa) {
        mesas.add(mesa);
    }
    @SuppressWarnings("unchecked")
    public void fromXML(Document xml) {
        Element root = xml.getRootElement();
        Element listMesasNode = root.getChild(NODE_LIST_TABLE);
        // setType(listMesasNode.getAttributeValue("type"));
        List<Element> tableNode = listMesasNode.getChildren("table");
        Mesa mesa = null;
        for (Element nodeTables : tableNode) {
            mesa = new Mesa();
            mesa.fromXml(nodeTables);
//            mesa.setMesaId(Integer.parseInt(nodeTables.getAttributeValue("id")));
//            mesa.setStatus(Boolean.parseBoolean(nodeTables.getChild("status").getValue()));
//            userNode = nodeTables.getChildren("usuario");
//            for (Element nodeUsers : userNode) {
//                warUser = new WarUser();
//                mesa.addPlayer(warUser.fromXml(nodeUsers));
//            }
//            Element nodeWatcher = nodeTables.getChild("watchers");
//            watcherNode = nodeWatcher.getChildren("usuario");
//            for (Element nodeWatchers : watcherNode) {
//                user = new User();
//                mesa.setWatchers(user.fromXml(nodeWatchers));
//            }
            addMesa(mesa);
        }
    }
    public void toXML(Element root) {
        Element listMesasNode = new Element(NODE_LIST_TABLE);
        Element tableNode = null;
        for (Mesa nodemesas : mesas) {
            tableNode = new Element("table");
//            tableNode.setAttribute("id", String.valueOf(nodemesas.getMesaId()));
//            statusNode = new Element("status");
//            statusNode.addContent(String.valueOf(nodemesas.isStatus()));
            nodemesas.toXml(tableNode);
            listMesasNode.addContent(tableNode);
        }
        root.addContent(listMesasNode);
    }
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof ListTableResponse)) {
            return false;
        }
        ListTableResponse otherObj = (ListTableResponse) obj;
        if (otherObj.getType().equals(getType())) {
            return true;
        }
        return false;
    }
}
